﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace quanlysinhvien
{
    public partial class FormView : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataReader sdr;
        SqlDataAdapter da;
        DataSet ds;
        
        public FormView()
        {
            InitializeComponent();
        }
        private void LoadFromfrmList()
        {
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select Student_Name,Student_Code,Class_Code,Faculty_Name,Student_DOB,Student_Sex,Current_Address,Native_Place,Contact_Address,Father_Name,Father_Job,Mother_Name,Mother_Job from TblStudents where Student_Code='" + FrmListStudent.studentcode + "'";
            cmd = new SqlCommand(sql, conn);
            sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                txtStuName.Text = sdr.GetString(0);
                txtStuCod.Text = sdr.GetString(1);
                txtClaCod.Text = sdr.GetString(2);
                txtFaculty.Text = sdr.GetString(3);
                txtBirthday.Text = sdr.GetDateTime(4).ToString();
                txtSex.Text = sdr.GetString(5);
                txtCurAdd.Text = sdr.GetString(6);
                txtNatPla.Text = sdr.GetString(7);
                txtConAdd.Text = sdr.GetString(8);
                txtFatNam.Text = sdr.GetString(9);
                txtFatJob.Text = sdr.GetString(10);
                txtMotNam.Text = sdr.GetString(11);
                txtMotJob.Text = sdr.GetString(12);
            }
        
        }
        private void FormView_Load(object sender, EventArgs e)
        {
            LoadFromfrmList();
            conn = Connect.getConnection();
            conn.Open();
            string sql = "select Student_Name,Student_Code,Class_Code,Faculty_Name,Student_DOB,Student_Sex,Current_Address,Native_Place,Contact_Address,Father_Name,Father_Job,Mother_Name,Mother_Job from TblStudents where Student_Code='" + FrmListStudent.studentcode + "'";
            cmd = new SqlCommand(sql, conn);
            sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                txtStuName.Text = sdr.GetString(0);
                txtStuCod.Text = sdr.GetString(1);
                txtClaCod.Text = sdr.GetString(2);
                txtFaculty.Text = sdr.GetString(3);
                txtBirthday.Text = sdr.GetDateTime(4).ToString();
                txtSex.Text = sdr.GetString(5);
                txtCurAdd.Text = sdr.GetString(6);
                txtNatPla.Text = sdr.GetString(7);
                txtConAdd.Text = sdr.GetString(8);
                txtFatNam.Text = sdr.GetString(9);
                txtFatJob.Text = sdr.GetString(10);
                txtMotNam.Text = sdr.GetString(11);
                txtMotJob.Text = sdr.GetString(12);
            }


            // load dgv

                conn = Connect.getConnection();
                conn.Open();
                String strSql = "select TblStu_Sub.Student_Code[Student Code],TblSubject.Subject_Code[Subject Code],TblSubject.Subject_Name[Subject Name],TblSubject.Term,TblSubject.No_Of_Credit[No of Credit],TblStu_Sub.Mark[Mark] from TblSubject,TblStu_Sub where TblSubject.Subject_Code=TblStu_Sub.Subject_Code and TblStu_Sub.Student_Code='" + FrmListStudent.studentcode + "'";
                //MessageBox.Show(strSql);
                cmd = new SqlCommand(strSql, conn);
                da = new SqlDataAdapter(cmd);
                ds = new DataSet();
                da.Fill(ds, "student");
                dataGridView1.DataSource = ds.Tables[0];
                conn.Close();


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            FormSearch_Student FormSearch_Student = new FormSearch_Student();
            FormSearch_Student.Show();
            
            this.Hide();

        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
        
    }
}
